package pl.veneo.wigprofit.view.elements {
	import pl.veneo.wigprofit.model.Configuration;
	import pl.veneo.wigprofit.model.PeriodData;

	import flash.display.Sprite;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;

	/**
	 * @author Tomasz Swierszcz, fiodorkrk@gmail.com
	 */
	public class ChartScales extends Sprite {
		private var _width : Number;
		private var _height : Number;
		private var _percents : Sprite;
		private var _indexes : Sprite;
		
		private var _period : PeriodData;

		public function ChartScales(width : Number, height : Number) {
			_height = height;
			_width = width;

			graphics.lineStyle(0, Configuration.getInstance().strongColor);
			var posY : Number;

			for (var i : int = 0; i < 10; i++) {
				posY = _height / 10 * i;
				graphics.moveTo(0, posY);
				graphics.lineTo(_width, posY);
			}

			graphics.lineStyle(1, Configuration.getInstance().lightColor);
			graphics.moveTo(0, 0);
			graphics.lineTo(0, _height);
			graphics.lineTo(_width, _height);
			graphics.lineTo(_width, 0);

		}

		public function draw(period : PeriodData) : void {
			_period = period;

			if (_percents)
				removeChild(_percents);
			_percents = addChild(new Sprite()) as Sprite;
			_percents.mouseEnabled = false;

			if (_indexes)
				removeChild(_indexes);
			_indexes = addChild(new Sprite()) as Sprite;
			_indexes.mouseEnabled = false;
			_indexes.x = _width;

			var min : Number = period.minIndex;
			var max : Number = period.maxIndex;

			var overscale : Number = (max - min) * 0.05;
			min -= overscale;
			max += overscale;

			var posY : Number;
			var tf : TextField;
			for (var i : int = 0; i < 10; i++) {
				posY = _height / 10 * i;

				tf = new TextField();
				tf.defaultTextFormat = getStyleForPercent();
				tf.text = (100 - i * 10) + "%";
				tf.multiline = false;
				tf.selectable = false;
				tf.autoSize = TextFieldAutoSize.LEFT;
				tf.x -= tf.textWidth + 5;
				tf.y = posY - tf.textHeight / 1.6;
				_percents.addChild(tf);

				tf = new TextField();
				tf.defaultTextFormat = getStyleForIndex();
				tf.text = formatIndexValue((max - ((max - min) / 10) * i));
				tf.multiline = false;
				tf.selectable = false;
				tf.autoSize = TextFieldAutoSize.LEFT;
				tf.x = 4;
				tf.y = posY - tf.textHeight / 1.6;
				_indexes.addChild(tf);
			}
		}

		private function formatIndexValue(value : Number) : String {
			var before : String = String(value);
			var out : String = before.split('.')[0];
			var end : String = before.split('.')[1];
			end = end.length == 1 ? "0" + end : end;
			end = end.length > 2 ? end.substr(0, 2) : end;
			return out + '.' + end;
		}

		private function getStyleForPercent() : TextFormat {
			var tf : TextFormat = new TextFormat();
			// tf.align = TextFormatAlign.RIGHT;
			tf.size = 9;
			tf.font = "Tahoma";
			tf.color = Configuration.getInstance().percentColor;
			return tf;
		}

		private function getStyleForIndex() : TextFormat {
			var tf : TextFormat = new TextFormat();
			// tf.align = TextFormatAlign.RIGHT;
			tf.size = 9;
			tf.font = "Tahoma";
			tf.color = Configuration.getInstance().indexColor;
			return tf;
		}

	}
}
